package com.js.controller;

import com.js.mapper.StudentMapper;

import com.js.pojo.Student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("student")
public class StudentLogin {
    @Autowired
    private StudentMapper studentMapper;
    //跳转登录界面
    @RequestMapping("/jumpLogin")
    public String jumpLogin(HttpSession session){
        if (session.getAttribute("studentName")!=null){
            return "student/backstage";
        }
        return "student/studentLogin";
    }
    @RequestMapping("/jumpFalse")
    public String jumpFalse(){
        return "student/codeFalse";
    }
    //登录时提交form表单验证的请求
    @RequestMapping("/login")
    public String login(Student student, HttpSession session, Model model){
        Student queryByAccount = studentMapper.queryStudentByAccount(student.getAccount());
        if (queryByAccount==null){
            return "redirect:/student/jumpFalse";
        }
        if (queryByAccount.getPassword().equals(student.getPassword())){
            session.setAttribute("studentName",queryByAccount.getName());
            session.setAttribute("QStudent",queryByAccount);
            return "student/backstage";
        }else {
            return "redirect:/student/jumpFalse";
        }
    }
    //退出登录
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.invalidate();
        return  "../index";
    }
}
